//
//  JUFNMainViewController.swift
//  WiBoPM
//
//  Created by 陈俊峰 on 2017/1/4.
//  Copyright © 2017年 陈俊峰. All rights reserved.
/*
 1.创建tabbar的样式
 
 */

import UIKit

class JUFNMainViewController: UITabBarController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //创建一个自定义的tabbar
        let jufnTabbar = JUFNTabBar()
        
        self.setValue(jufnTabbar, forKey: "tabBar")
        jufnTabbar.callBackClosure = {
            print("ha")
            let composeView = JUFNComposeBtnView()
            composeView.showShotView(targetVC: self)
            }
            //截屏,和添加到子视图的view上的逻辑应该在自定义的view中执行,这是自定义视图自己的逻辑
//          let backgroundImage = self.SnapShotCurrentScreen()
//            
//            let snipScreen = UIImageView(image: backgroundImage)
//            UIApplication.shared.keyWindow?.addSubview(snipScreen)
//            snipScreen.snp.makeConstraints({ (make) in
//                make.center.equalTo(UIApplication.shared.keyWindow!)
//            })
            
        
       
        
        // Do any additional setup after loading the view.
        addChildViewControllers(vc: JUFNHomeTableViewController(), imageName: "tabbar_home", title: "首页",viewTag: 0)
        addChildViewControllers(vc: JUFNMessageTableViewController(), imageName: "tabbar_message_center", title: "消息",viewTag: 1)
        addChildViewControllers(vc: JUFNDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现",viewTag: 2)
        addChildViewControllers(vc: JUFNProfileTableViewController(), imageName: "tabbar_profile", title: "我的",viewTag: 3)
        
        
    }

    func addChildViewControllers(vc:UIViewController,imageName:String,title:String,viewTag:Int){
        
        
        vc.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.tag = viewTag
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.title = title
        vc.navigationItem.title = "新浪微博"
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        //        vc.tabBarItem.imageInsets这个可以设置图片和文字的相对位置
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        let nav = JUFNNavViewController()
        nav.addChildViewController(vc)
        //上面的方式和下面顺便创建控制器的方法差别在于,直接添加子控制器时不会调用pushViewController方法.而添加根控制器的实质是push控制器上去的.
        //        let nav = JUFNNavViewController(rootViewController: vc)
        addChildViewController(nav)
    }
    //点击按钮,使得产生动画效果,tabBar的代理方
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        var index = 0
        for tabBarSubV in tabBar.subviews {
            if tabBarSubV.isKind(of: NSClassFromString("UITabBarButton")!) {
                print(tabBarSubV)
                if item.tag == index{
                    print(item.tag,index)
                    for aniItem in tabBarSubV.subviews {
                        if aniItem.isKind(of: NSClassFromString("UITabBarSwappableImageView")!) {
                        //这里做动画
                            UIView.animate(withDuration: 0.25, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 3, options: [], animations: { 
                                aniItem.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
                            }, completion: { (_) in
                                UIView.animate(withDuration: 0.25, animations: { 
                                    aniItem.transform = CGAffineTransform.identity
                                })
                            })
                        
                        }
                    }
                }
                index += 1
            }
            
        }
    }
    
}
